package hlhj.fhp.supreme.activitys

import android.content.Context
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.ListView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import fhp.hlhj.giantfold.utils.LogUtil
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.adapters.HotListAdp
import hlhj.fhp.supreme.adapters.ReplyAdp
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.javabean.CommentBean
import hlhj.fhp.supreme.javabean.CommentMultiBean
import hlhj.fhp.supreme.javabean.IsTeacherBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.activity_option_comment_aty.*
import java.util.ArrayList

class OptionCommentAty : BaseAty() {
    private lateinit var adp:BaseMultiItemQuickAdapter<CommentMultiBean,BaseViewHolder>
    private lateinit var hotAdp:BaseQuickAdapter<CommentBean.DataBean.HotBean,BaseViewHolder>//热评
    private lateinit var regulerAdp:BaseQuickAdapter<CommentBean.DataBean.NewBean,BaseViewHolder>//普通品论
    private val datas=ArrayList<CommentMultiBean>()
    private var img=""
    private var tittle=""
    private var des=""
    private var objectId=""
    private var pid=""
    var isTeacher=false;
    /*头部*/
    val headBean = CommentMultiBean(1)
    /*热评*/
    val hotBean = CommentMultiBean(2)
    /*普评*/
    val regularBean = CommentMultiBean(3)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
    }
    override fun getContentId(): Int = R.layout.activity_option_comment_aty
    override fun initView() {

        img=intent.getStringExtra("img")
        tittle=intent.getStringExtra("tittle")
        des=intent.getStringExtra("des")
        objectId=intent.getStringExtra("oid")


        OkGo.post<IsTeacherBean>(Urls.BaseUrl+Urls.ISTEACH)
                .execute(object :JsonCallBack<IsTeacherBean>(this){
                    override fun onSuccess(response: Response<IsTeacherBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            if (bean?.data.status==2){
                                isTeacher=true
                            }else{
                            }
                        }
                    }
                })

        hotAdp=object:BaseQuickAdapter<CommentBean.DataBean.HotBean,BaseViewHolder>(R.layout.option_comment_item,hotBean.hotDatas){
            override fun convert(helper: BaseViewHolder?, item: CommentBean.DataBean.HotBean?) {
                Glide.with(mContext).load(item?.avatar).asBitmap().into(helper?.getView(R.id.itemIcon))
                helper?.setText(R.id.itemName,item?.full_name?.substring(item?.full_name.length-1,item?.full_name.length)+"*")
                helper?.setText(R.id.itemTime,MyUtils.convertTimeToCustom(item?.create_time?.toLong()!!))
                helper?.setText(R.id.itemContent,item?.content)
                val listView = helper?.getView<ListView>(R.id.listView)
                val btRep = helper?.getView<ImageView>(R.id.btRep)

                LogUtil.log("设置评论的listView")
                if (item?.son!=null){
                    listView?.visibility= View.VISIBLE
                    val hotListAdp = HotListAdp(item?.son as ArrayList<CommentBean.DataBean.HotBean.SonBeanX>, mContext)
                    listView?.adapter=hotListAdp
                }else{
                    listView?.visibility=View.GONE
                }

                btRep?.setOnClickListener {
                    if (isTeacher){
                        etContent.setFocusable(true);
                        etContent.setFocusableInTouchMode(true);
                        etContent.requestFocus();
                        pid=item?.id.toString()
                        val inputManager = etContent.getContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                        inputManager.showSoftInput(etContent,0)
                    }else{
                        MyUtils.toast("只有老师能够回复别人")
                    }
                }
            }
        }

        regulerAdp=object:BaseQuickAdapter<CommentBean.DataBean.NewBean,BaseViewHolder>(R.layout.option_comment_item,regularBean.regulerDatas){
            override fun convert(helper: BaseViewHolder?, item: CommentBean.DataBean.NewBean?) {
                Glide.with(mContext).load(item?.avatar).asBitmap().into(helper?.getView(R.id.itemIcon))

                helper?.setText(R.id.itemName,item?.full_name?.substring(item?.full_name.length-1,item?.full_name.length)+"*")
                helper?.setText(R.id.itemTime,MyUtils.convertTimeToCustom(item?.create_time?.toLong()!!))
                helper?.setText(R.id.itemContent,item?.content)
                val listView = helper?.getView<ListView>(R.id.listView)
                if (item?.son!=null){
                    val replyAdp = ReplyAdp(mContext, item?.son as ArrayList<CommentBean.DataBean.NewBean.SonBean>)
                    listView?.adapter=replyAdp
                }
                val btRep = helper?.getView<ImageView>(R.id.btRep)

                btRep?.setOnClickListener {
                    if (isTeacher){
                        etContent.setFocusable(true);
                        etContent.setFocusableInTouchMode(true);
                        etContent.requestFocus();
                        pid=item?.id.toString()
                        val inputManager = etContent.getContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                        inputManager.showSoftInput(etContent,0)
                    }else{
                        MyUtils.toast("只有老师能够回复别人")
                    }
                }
            }
        }

        datas.add(headBean)
        datas.add(hotBean)
        datas.add(regularBean)

        adp=object :BaseMultiItemQuickAdapter<CommentMultiBean,BaseViewHolder>(datas){
            init {
                addItemType(1,R.layout.option_comment_head)
                addItemType(2,R.layout.option_comment_ar)
                addItemType(3,R.layout.option_comment_ar)
            }
            override fun convert(helper: BaseViewHolder?, item: CommentMultiBean?) {
                when(helper?.itemViewType){
                    1->{
                        helper?.setText(R.id.tvTittle,tittle)
                        helper?.setText(R.id.tvInfo,des)
                        Glide.with(mContext).load(img).into(helper?.getView(R.id.img))
                    }
                    2->{
                        helper?.setText(R.id.tvTittle,"热评")
                        val recyclerView = helper?.getView<RecyclerView>(R.id.recyclerview)
                        recyclerView.adapter=hotAdp
                        recyclerView.layoutManager=LinearLayoutManager(this@OptionCommentAty,LinearLayout.VERTICAL,false)
                    }
                    3->{
                        helper?.setText(R.id.tvTittle,"最新")
                        val recyclerView = helper?.getView<RecyclerView>(R.id.recyclerview)
                        recyclerView.adapter=regulerAdp
                        recyclerView.layoutManager=LinearLayoutManager(this@OptionCommentAty,LinearLayout.VERTICAL,false)
                    }
                }
            }
        }
        recyclerview.adapter=adp
        recyclerview.layoutManager=LinearLayoutManager(this,LinearLayout.VERTICAL,false)
        disPatchData()
    }

    private fun disPatchData() {
        OkGo.get<CommentBean>(Urls.BaseUrl + Urls.CommentList)
    //                .params("object_id",2)
    //                .params("type",1)
                .params("object_id", intent.getStringExtra("oid"))
                .params("type", intent.getIntExtra("type", 1))
                .execute(object : JsonCallBack<CommentBean>(this) {
                    override fun onSuccess(response: Response<CommentBean>?) {
                        val bean = response?.body()
                        if (bean?.code == 1) {
                            if (bean?.data != null) {
                                if (bean?.data.hot != null) {
                                    hotBean.hotDatas.addAll(bean?.data.hot)
                                    hotAdp.notifyDataSetChanged()
                                }

                                if (bean?.data.newX != null) {
                                    regularBean.regulerDatas.addAll(bean?.data.newX)
                                    regulerAdp.notifyDataSetChanged()
                                }
                            }
                        }
                    }
                })
    }

    override fun initListener() {
        showEt.setOnClickListener {
            pid=""
            val inputManager = etContent.getContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            inputManager.showSoftInput(etContent,0)
        }
        btExit.setOnClickListener {
            finish()
        }
        btComment.setOnClickListener {
            if (!etContent.text.isEmpty()){
                OkGo.post<BaseBean>(Urls.BaseUrl+Urls.HUIFU)
                        .params("object_id",objectId)
                        .params("type",1)
                        .params("content",etContent.text.toString())
                        .params("parent_id",pid)
                        .execute(object:JsonCallBack<BaseBean>(this@OptionCommentAty){
                            override fun onSuccess(response: Response<BaseBean>?) {
                                val bean = response?.body()
                                if (bean?.code==1){
                                    etContent.setText("")
                                    hotBean.hotDatas.clear()
                                    regularBean.regulerDatas.clear()
                                    disPatchData()
                                }else{
                                    MyUtils.toast(bean?.msg)
                                }
                            }
                        })
            }
        }

    }
}
